﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessComponents.Managers;
using BusinessComponents;

namespace WaterfrontLodge.WebForms.RoomTypeMgt
{
    public partial class RoomTypeAddEdit : System.Web.UI.Page 
    {
        RoomType rType;
        RoomTypeNavigationController ctrNavigation; 
        #region PageLoad

        protected void Page_Load(object sender, EventArgs e)
        {
            ctrNavigation = new RoomTypeNavigationController();
            ctrNavigation.PageLoad();
            ctrNavigation.CurrentPage = "RoomTypeAddEdit.aspx";
           
            if (!IsPostBack)
            {

                if (Request.QueryString["TypeID"] != string.Empty && Request.QueryString["TypeID"] != null)
                {
                    lblTypeCode.Text = Request.QueryString["TypeID"].ToString();
                    LoadRoomTypeDetails();
                }
            }
           
        }

        #endregion

        #region EventHandler

        protected void btnNext_Click(object sender, EventArgs e)
        {
            if (!IsValid)
            {
                return;
            }
            else
            {
                string strErrorMsg = ManagerFactory.RoomTypeMgr.ValidateRoomTypeInfo(txtDescription.Text, txtRoomSize.Text, txtMaxOccupant.Text, txtStdPrice.Text);
                if (strErrorMsg != string.Empty)
                {
                    lblErrorMsg.Text = strErrorMsg;
                    return;
                }
                else
                {
                    int intTypeID = int.Parse ( lblTypeCode.Text) ;
                    bool retRoomType = ManagerFactory.RoomTypeMgr.CheckDuplicateRoomType(txtDescription.Text, intTypeID);
                    if (retRoomType)
                    {
                        lblErrorMsg.Text = "The Room Type Description is already existed!";
                    }
                    else
                    {
                        if (lblTypeCode.Text != string.Empty)
                        {
                            ctrNavigation.RoomTypeDetails.TypeCode = int.Parse(lblTypeCode.Text);
                        }
                        ctrNavigation.RoomTypeDetails.Description = txtDescription.Text;
                        ctrNavigation.RoomTypeDetails.RoomSize = txtRoomSize.Text;
                        ctrNavigation.RoomTypeDetails.MaxOccupant = Convert.ToInt32(txtMaxOccupant.Text.ToString());
                        ctrNavigation.RoomTypeDetails.Remarks = txtRemarks.Text;
                        ctrNavigation.RoomTypeDetails.StdPrice = Convert.ToInt32(txtStdPrice.Text.ToString()) * 100;
                        Response.Redirect("RoomTypeAddEdit1.aspx");
                    }
                  
                }
            }
           
        }
      
        #endregion

        #region Functions

        public void LoadRoomTypeDetails()
        {
             rType = ManagerFactory.RoomTypeMgr.GetRoomType(Convert.ToInt32(lblTypeCode.Text.ToString()));
            if (rType != null)
            {
                txtDescription.Text = rType.Description.ToString();
                txtRoomSize.Text = (rType.RoomSize == null ? "" : rType.RoomSize.ToString());
                txtMaxOccupant.Text = rType.MaxOccupant.ToString();
                txtRemarks.Text = (rType.Remarks == null ? "" : rType.Remarks.ToString());
                int intPrice = rType.StdPrice/100;
                txtStdPrice.Text = intPrice.ToString ();
            }
            
        }

        #endregion

      

       
    }
}